MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Architecture and design

Introduction#

Three pieces make up the Assignment Solver extension:

  • Background service worker: messaging, task coordination, platform adapters.
  • Content script: DOM read/write for extraction and answer application.
  • Side panel UI: start solves, edit preferences, show results.

Messaging, adapters, and how those layers fit together are the focus here.

Project structure#

Extension layers:

  • background/: background service worker entry point, message routing, and platform adapters
  • content/: content script entry point and DOM interaction utilities
  • ui/: side panel UI entry point and controllers
  • core/: shared message types and utilities
  • platform/: cross-browser adapters for browser APIs
  • services/: domain services (e.g., Gemini AI)
  • public/: side panel HTML and assets

Core components#

  • Background service worker: initializes platform adapters, creates services, registers message handlers, and opens the side panel.
  • Content script: listens for messages, extracts page HTML and images, scrolls for screenshots, applies answers, and submits assignments.
  • UI side panel: waits for background readiness, initializes controllers, and coordinates the solve flow.

Key cross-cutting concerns:

  • Message types and retry logic: centralized in core/messages.js so consistent communication semantics across components.
  • Platform adapters: unify Chrome/Firefox differences for runtime, tabs, panel, and browser detection.

Architecture overview#

The system uses a unidirectional message flow with explicit phases:

  1. UI triggers a solve operation.
  2. Background coordinates extraction, AI processing, and application.
  3. Content script performs DOM operations and returns results.
  4. UI renders progress and results.

Detailed component analysis#

Background service worker#

Responsibilities:

  • Initialize platform adapters and services.
  • Register message handlers for extraction, screenshots, Gemini requests, answer application, and submission.
  • Manage side panel behavior and icon click actions.
  • Route incoming messages to appropriate handlers.

Design highlights:

  • Dependency injection via factory functions for adapters and services.
  • Centralized message router keeps consistent async handling and response semantics.
  • Reliable logging and error handling for cross-browser environments.

Content script#

Responsibilities:

  • Listen for messages from the background.
  • Extract page HTML and images, compute scroll info, scroll to positions, and apply answers.
  • Submit assignments programmatically.

Design highlights:

  • Factory-based initialization of extractor and applicator.
  • Detailed DOM traversal and selection strategies for diverse NPTEL layouts.
  • Defensive image extraction with CORS handling and size filtering.

UI side panel#

Responsibilities:

  • Initialize UI, controllers, and state managers.
  • Coordinate the solve lifecycle: extraction, AI processing, answer filling, and optional submission.
  • Provide progress tracking and results display.
  • Handle settings and API key management.

Design highlights:

  • Wait-for-background mechanism with exponential backoff for Firefox compatibility.
  • Recursive splitting and merging for handling MAX_TOKENS errors from Gemini.
  • Modular controllers for detection, progress, settings, and solve.

Message passing system#

Components communicate via a well-defined set of message types and a retry mechanism:

  • Types: PING, GET_PAGE_HTML, GET_PAGE_INFO, APPLY_ANSWERS, SUBMIT_ASSIGNMENT, EXTRACT_HTML, CAPTURE_FULL_PAGE, GEMINI_REQUEST, GEMINI_DEBUG, SCROLL_INFO, SCROLL_TO, TAB_UPDATED.
  • Retry logic: exponential backoff with connection error detection to handle transient failures, especially in Firefox.

Cross-Browser compatibility strategy#

  • Unified browser API: webextension-polyfill is used to expose a consistent browser.* API across Chrome and Firefox.
  • Feature detection: runtime detection of Chrome vs Firefox and availability of optional APIs.
  • Panel abstraction: a single adapter handles sidePanel (Chrome) and sidebarAction (Firefox) differences.
  • Manifest configuration: manifest v3 with permissions and side panel defaults.

Dependency injection pattern and factory functions#

  • Platform adapters: createRuntimeAdapter, createTabsAdapter, createScriptingAdapter, createPanelAdapter encapsulate browser differences.
  • Services: createGeminiService composes runtime and logger; createScreenshotService composes tabs and scripting.
  • Controllers: createSolveController, createDetectionController, createProgressController, createSettingsController accept dependencies via factories.
  • Content services: createExtractor and createApplicator isolate DOM operations.

Data flow through extraction-analyze-apply phases#

  • Extraction: UI requests page HTML and images; background forwards to content script; content script locates assignment containers and images; background captures full-page screenshots; Gemini extracts structured questions.
  • Analyze: UI requests AI analysis; Gemini parses extraction with images/screenshots; recursive splitting mitigates token limits; results merged.
  • Apply: UI fills answers; background sends APPLY_ANSWERS; content script applies selections/text; optional auto-submit triggers SUBMIT_ASSIGNMENT.

Dependency analysis#

The system has loose coupling and high cohesion:

  • Background depends on platform adapters and services; handlers depend on adapters and logger.
  • Content script depends on extractor and applicator; both depend on logger.
  • UI depends on runtime adapter, storage, and controllers; solve controller depends on Gemini service and state.

Performance considerations#

  • Token limit handling: recursive splitting of HTML and questions reduces payload sizes to fit model constraints.
  • Image handling: filtering small images and skipping CORS-impacted images avoids unnecessary overhead.
  • Retry strategy: exponential backoff minimizes repeated failures and improves reliability on slower browsers.
  • Screenshot capture: targeted full-page capture reduces bandwidth and processing time.
  • DOM operations: batched application with progress updates prevents UI blocking.

Troubleshooting guide#

Common issues and remedies:

  • Background not ready (Firefox): UI waits for PING response with retries; ensure extension reload and page refresh.
  • Message port errors: sendMessageWithRetry detects transient connection errors and retries; check network/API key validity.
  • MAX_TOKENS errors: UI splits HTML or questions recursively; adjust model preferences or reduce content.
  • CORS image extraction: skipped images are logged; verify image origins and avoid external resources when possible.
  • Panel opening failures: panel adapter handles Firefox vs Chrome differences; verify permissions and API availability.

Conclusion#

Background, content script, and UI stay loosely coupled through messages and injected adapters. That split is what makes Chrome/Firefox and test doubles workable.